package cn.edu.sasu.service.Impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.domain.dto.PatientEducationActivityDTO;
import cn.edu.sasu.domain.entity.PatientEducationActivity;
import cn.edu.sasu.domain.entity.SysDoctor;
import cn.edu.sasu.domain.entity.SysHospital;
import cn.edu.sasu.mapper.PatientEducationActivityMapper;
import cn.edu.sasu.service.PatientEducationActivityService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author pushuqi
 */
@Service
public class PatientEducationActivityServiceImpl implements PatientEducationActivityService{
    @Resource
    private PatientEducationActivityMapper activityMapper;


    @Override
    public LayuiRespResult activityList(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<PatientEducationActivity> acDepts = activityMapper.activityList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = activityMapper.countActivities(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize,acDepts);
    }

    @Override
    public List<PatientEducationActivity> getActivities(Integer deleteFlag) {
        return activityMapper.getAllActivity(deleteFlag);
    }

    @Override
    public RespResult delActivityById(Integer activityId) {
        activityMapper.updateActivityDeleteFlagById(activityId,Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult batchdel(Integer[] ids) {
        activityMapper.updateActivityDeleteFlagByIds(Arrays.asList(ids),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public LayuiRespResult search(Integer page, Integer limit, String activityName,Byte activityStatus) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<PatientEducationActivity> activities = activityMapper.searchActivityList(Constant.DELETE_FLAG_YES,offset,rows,activityName,activityStatus);
        Integer totalSize = activityMapper.countSearchActivity(Constant.DELETE_FLAG_YES,activityName);
        return LayuiRespResult.success(totalSize,activities);
    }

    @Override
    public RespResult save(PatientEducationActivity patientEducationActivity) {
        PatientEducationActivity activity=activityMapper.findActivityMapperByActivityName(patientEducationActivity.getActivityName());
        if(activity != null){
            return RespResult.error("该患教名已经存在，请重新输入！");
        }
        // 对象拷贝
        PatientEducationActivity patientEducationActivity1 = BeanUtil.copyProperties(patientEducationActivity,PatientEducationActivity.class);
        activityMapper.save(patientEducationActivity1);
        return RespResult.success("添加成功");
    }

    @Override
    public RespResult update(PatientEducationActivityDTO patientEducationActivityDTO) {
        PatientEducationActivity activity = BeanUtil.copyProperties(patientEducationActivityDTO,PatientEducationActivity.class);
       Integer update =  activityMapper.update(activity);
       if(update<=0){
           return RespResult.error("修改失败");
       }
        return  RespResult.success("修改成功");
    }
}
